package com.wenzhi.java_join_kotlin.service.impl;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonParser;
import com.wenzhi.java_join_kotlin.service.ISeatManagerService;

import java.util.Arrays;
import java.util.Objects;
import java.util.PriorityQueue;


public class SeatManagerServiceImpl implements ISeatManagerService {
    @Override
    public String[] runSeatReservationManager() {
        String command = "[\"SeatManager\", \"reserve\", \"reserve\", \"unreserve\", \"reserve\", \"reserve\", \"reserve\", \"reserve\", \"unreserve\"]";
        String data = "[[5], [], [], [2], [], [], [], [], [5]]";

        String[] commandArray = Wrapper.stringToStringArray(command);
        int[][] dataArray2 = Wrapper.stringToInt2dArray(data);

        System.out.println("commandArray: " + Arrays.toString(commandArray));
        System.out.println("dataArray2: " + Arrays.deepToString(dataArray2));
        
        String[] result = new String[commandArray.length];
        SeatManager sm = null;
        for (int i = 0; i < commandArray.length; i++) {
            if (Objects.equals(commandArray[i], "SeatManager")) {
                sm = new SeatManager(dataArray2[i][0]);
                result[i] = null;
            } else if (Objects.equals(commandArray[i], "reserve")) {
                assert sm != null;
                int n = sm.reserve();
                result[i] = Integer.toString(n);
            } else if (Objects.equals(commandArray[i], "unreserve")) {
                assert sm != null;
                sm.unReserve(dataArray2[i][0]);
                result[i] = null;
            }
        }

        System.out.println("result: " + Arrays.toString(result));

        return result;

    }
}


class SeatManager {
    private final PriorityQueue<Integer> available;
    public SeatManager(int n) {
        available = new PriorityQueue<>();
        for (int i = 1; i <= n; i++) {
            available.offer(i);
        }
    }

    public int reserve() {
        if (available.isEmpty()) {
            throw new NullPointerException("available cannot be null");
            //return -1;
        }
        return available.poll();
    }

    public void unReserve(int seatNumber) {
        available.offer(seatNumber);
    }
}


/*
* https://leetcode.cn/problems/seat-reservation-manager/
* */
class Wrapper {
    public static String[] stringToStringArray(String line) {
        JsonArray jsonArray = JsonParser.parseString(line).getAsJsonArray();
        String[] arr = new String[jsonArray.size()];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = jsonArray.get(i).getAsString();
        }
        return arr;
    }

    public int[] stringToIntegerArray(String input) {
        input = input.trim();
        input = input.substring(1, input.length() - 1);
        if (input.isEmpty()) {
            return new int[0];
        }

        String[] parts = input.split(",");
        int[] output = new int[parts.length];
        for(int index = 0; index < parts.length; index++) {
            String part = parts[index].trim();
            output[index] = Integer.parseInt(part);
        }
        return output;
    }

    public static int[][] stringToInt2dArray(String input) {
         Gson gson = new Gson();
        JsonArray jsonArray = JsonParser.parseString(input).getAsJsonArray();

        if (jsonArray.isEmpty()) {
            return new int[0][0];
        }

        int[][] arr = new int[jsonArray.size()][];
        for (int i = 0; i < jsonArray.size(); i++) {
            JsonArray cols = jsonArray.get(i).getAsJsonArray();
            int[] row = gson.fromJson(cols, int[].class);
            arr[i] = row;
        }
        return arr;
    }


}